package com.enqualcomm.kids.view;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Environment;

import com.enqualcomm.kids.R;
import com.enqualcomm.kids.config.userdefault.AppDefault;
import com.enqualcomm.kids.config.userdefault.TerminalDefault;
import com.enqualcomm.kids.config.userdefault.UserTerminalDefault;
import com.enqualcomm.kids.manager.Controller;

import java.io.File;

import common.utils.MyLogger;

public class ImageUtils {

    // 缓存集合
//	private static Hashtable<String, SoftReference<Bitmap>>
//		mCacheHashTable = new Hashtable<String, SoftReference<Bitmap>>();

    /**
     * 根据id返回一个处理后的图片
     *
     * @return
     */
    public static Bitmap getImageBitmap(int width, int height, String imagePath) {

//		// 先去集合中取当前imageID 是否已经拿过图片, 如果集合中有, 说明已经拿过, 直接使用集合中的图片返回
//		SoftReference<Bitmap> softReference = mCacheHashTable.get(imagePath+width);
//		if(softReference != null) {
//			Bitmap bitmap = softReference.get();
//
//			if(bitmap != null) {
//				// 从内存中取
//				return bitmap;
//			}
//		}
//
//		// 如果集合中没有, 就调用getInvertImage得到一个图片, 需要向集合中保留一张, 最后返回当前图片
//		Bitmap invertImage = getInvertImage(width,height,imagePath);
//		// 在集合中存一份, 便于下次再取的时候直接去集合中取.
//		mCacheHashTable.put(imagePath+width, new SoftReference<Bitmap>(invertImage));
//		return invertImage;

        //不使用缓存，直接加载图片并返回
        return BitmapFactory.decodeFile(imagePath);
    }

    /**
     * 获取动物头像
     */
    public static Bitmap getPetImageBitmap(Context context, int petType) {
        BitmapFactory.decodeResource(context.getResources(), R.drawable.pet_cat_icon_normal);
        return null;
    }

    public static Bitmap getImageBitmap(Context context, String terminalid, String userterminalid, int gender) {
        String defaultIconPath;
        String boyIconPath;
        String girlIconPath;

        String imei = new TerminalDefault(terminalid).getConfig().imei;
        if (/*!AppDefault.isOldManCid(imei)*/!Controller.getInstance().isOldMainMode()) {
            defaultIconPath = AppDefault.getDefaultIconPath(context);
            boyIconPath = AppDefault.getBoyIconPath(context);
            girlIconPath = AppDefault.getGirlIconPath(context);
        } else {
            defaultIconPath = AppDefault.getDefaultIconPathOldMan(context);
            boyIconPath = AppDefault.getBoyIconPathOldMan(context);
            girlIconPath = AppDefault.getGirlIconPathOldMan(context);
        }

        UserTerminalDefault userTerminalDefault = new UserTerminalDefault(userterminalid);
        String path = userTerminalDefault.getIconPath();

        boolean changed = false;
        if (path == null) {//如果头像下载失败，头像路径就是null，下次要接着下载
            if (gender == 1) {
                return BitmapFactory.decodeFile(girlIconPath);
            } else if (gender == 2) {
                return BitmapFactory.decodeFile(boyIconPath);
            } else {
                return BitmapFactory.decodeFile(defaultIconPath);
            }
        } else {
            if (gender != 0 && defaultIconPath.equals(path)) {
                changed = true;
            } else if (gender != 1 && boyIconPath.equals(path)) {
                changed = true;
            } else if (gender != 2 && girlIconPath.equals(path)) {
                changed = true;
            }
        }

        if (!changed) {
            Bitmap bitmap = BitmapFactory.decodeFile(path);
            if (bitmap == null) {
                if (gender == 1) {
                    bitmap = BitmapFactory.decodeFile(girlIconPath);
                } else if (gender == 2) {
                    bitmap = BitmapFactory.decodeFile(boyIconPath);
                } else {
                    bitmap = BitmapFactory.decodeFile(defaultIconPath);
                }
            }
            return bitmap;
        } else {
            //如果性别被修改了，并且没有上传过自定义头像
            if (gender == 1) {
                //还需要保存新的图片路径
                userTerminalDefault.setIconPath(boyIconPath);
                return BitmapFactory.decodeFile(boyIconPath);
            } else if (gender == 2) {
                userTerminalDefault.setIconPath(girlIconPath);
                return BitmapFactory.decodeFile(girlIconPath);
            } else {
                userTerminalDefault.setIconPath(defaultIconPath);
                return BitmapFactory.decodeFile(defaultIconPath);
            }
        }
    }

    public static String getHeader(Context context, String terminalid, String userterminalid, int gender) {

        String defaultIconPath;
        String boyIconPath;
        String girlIconPath;

        String imei = new TerminalDefault(terminalid).getConfig().imei;
        if (/*!AppDefault.isOldManCid(imei)*/!Controller.getInstance().isOldMainMode()) {
            defaultIconPath = AppDefault.getDefaultIconPath(context);
            boyIconPath = AppDefault.getBoyIconPath(context);
            girlIconPath = AppDefault.getGirlIconPath(context);
        } else {
            defaultIconPath = AppDefault.getDefaultIconPathOldMan(context);
            boyIconPath = AppDefault.getBoyIconPathOldMan(context);
            girlIconPath = AppDefault.getGirlIconPathOldMan(context);
        }

        UserTerminalDefault userTerminalDefault = new UserTerminalDefault(userterminalid);
        String path = userTerminalDefault.getIconPath();

        boolean changed = false;
        if (path == null) {//如果头像下载失败，头像路径就是null，下次要接着下载
            if (gender == 1) {
                return girlIconPath;
            } else if (gender == 2) {
                return boyIconPath;
            } else {
                return defaultIconPath;
            }
        } else {
            if (gender != 0 && defaultIconPath.equals(path)) {
                changed = true;
            } else if (gender != 1 && boyIconPath.equals(path)) {
                changed = true;
            } else if (gender != 2 && girlIconPath.equals(path)) {
                changed = true;
            }
        }

        if (!changed) {
            if (new File(path).exists()) {
                return path;
            } else {
                if (gender == 1) {
                    return girlIconPath;
                } else if (gender == 2) {
                    return boyIconPath;
                } else {
                    return defaultIconPath;
                }
            }
        } else {
            //如果性别被修改了，并且没有上传过自定义头像
            if (gender == 1) {
                //还需要保存新的图片路径
                userTerminalDefault.setIconPath(boyIconPath);
                return boyIconPath;
            } else if (gender == 2) {
                userTerminalDefault.setIconPath(girlIconPath);
                return girlIconPath;
            } else {
                userTerminalDefault.setIconPath(defaultIconPath);
                return defaultIconPath;
            }
        }
    }
//	/**
//	 * 根据id返回一个处理后的图片
//	 * @return
//	 */
//	private static Bitmap getInvertImage(int width, int height,String imagePath) {
//
//		//先只加载其他的信息
//		BitmapFactory.Options opts=new BitmapFactory.Options();
//		opts.inJustDecodeBounds=true;
//		BitmapFactory.decodeFile(imagePath,opts);
//
//		int scaleX = opts.outWidth/width;
//		int scaleY = opts.outHeight/height;
//		if (scaleX>1||scaleY>1) {
//			//加载的比例
//			opts.inSampleSize= scaleX>scaleY?scaleX:scaleY;
//		}
//
//		//重新加载图片
//		opts.inJustDecodeBounds = false;
//		Bitmap sourceBitmap = BitmapFactory.decodeFile(imagePath,opts);
//		if (sourceBitmap==null) {//图片被删除
//			return getInvertImage(width, height, AppDefault.getDefaultIconPath(MyApplication.getInstance()));
//		}
//
//		//不需要倒影效果，直接返回
//		return sourceBitmap;
//	}


    public static String getweatherPicPath(Context context, String picCode) {
        String path = "file:///" + context.getFilesDir() + "/weather/" + picCode + ".png";
        return path;
    }
}
